package com.WISSC.controller;


import com.WISSC.bean.Massage;
import com.WISSC.service.WebPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 学员图片关联表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-05-05
 */
@Controller
@RequestMapping("/studentImage")
@Api(value = "test",tags = "test")
public class StudentImageController {

    @Autowired
    WebPageService webPageService;


    @RequestMapping(value = "index")
    public String index(Model model) {
        model.addAttribute("list",webPageService.selectAll());
        return "index";
    }

    public  static  String fileUpload(  MultipartFile file) throws FileNotFoundException {
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        //String path = "F:/test" ;
        File path   = new File(ResourceUtils.getURL("classpath:").getPath());
        //System.out.println("path:"+path);
        String  filePath=path.getParentFile().getParentFile()+File.separator+"uploads"+File.separator;
        //System.out.println("filePath:"+filePath);
        String fiilePath=getJarRootPath()+"/uploads/"+new Date().getTime()+fileName;
        //System.out.println("fiilePath:"+fiilePath);
        fiilePath=fiilePath.replace('\\','/');
        File dest = new File(fiilePath);
        //System.out.println("dest:"+dest);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            System.out.println("fiilePath:"+fiilePath);
            return  fiilePath ;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getJarRootPath() throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        //=> file:/root/tmp/demo-springboot-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
        System.out.println("ResourceUtils.getURL(\"classpath:\").getPath() -> "+path);
        //创建File时会自动处理前缀和jar包路径问题 => /root/tmp
        File rootFile = new File(path);
        if(!rootFile.exists()) {
            System.out.println("根目录不存在, 重新创建");
            rootFile = new File("");
            System.out.println("重新创建的根目录: "+rootFile.getAbsolutePath()); }
        System.out.println("项目根目录: "+rootFile.getAbsolutePath());
        //获取的字符串末尾没有分隔符 /
        return rootFile.getAbsolutePath();
    }





}
